package algotithm.leetcode.dp.test740;

/**
 * @author 25068
 * @Date 2021/5/5 10:13
 * @Title 740. 删除并获得点数
 */
public class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] nums = {3,2,4};
        int ans = solution.deleteAndEarn(nums);
        System.out.println(ans);
    }

    public int deleteAndEarn(int[] nums) {
        int maxVal = 0;
        for (int val : nums) {
            maxVal = Math.max(maxVal,val);
        }
        int[] sum = new int[maxVal + 1];
        for (int val : nums) {
            sum[val] += val;
        }
        return myrob(sum);
    }

    public int myrob(int[] num) {
        int n = num.length;
        if (n == 0) {
            return num[0];
        }
        int first = num[0];
        int second = Math.max(num[0],num[1]);
        for (int i = 2; i < n; i++) {
            int tmp = second;
            second = Math.max(first + num[i], second);
            first = tmp;
        }
        return second;
    }


}
